/*
 * QTreeViewAdapter.cpp  0.4.0 / Nov 30, 2010
 *
 * Copyright (c) 2010 - 2011, cute4j.com and contributors.
 *
 * This software is licensed under the LGPL version 2.1 license.
 * A copy of this license is included in the file LICENSE-LGPL.txt.
 */
#include "QTreeViewAdapter.h"

#include <QHeaderView>

#include "TreeDelegate.h"
#include "TreeModel.h"

// QTreeViewAdapter :: Public Constructor. -------------------------------------

/*!
 * \brief Constructs new QTreeViewAdapter object.
 *
 * \param delegate the TreeDelegate pointer
 */
QTreeViewAdapter::QTreeViewAdapter(TreeDelegate *delegate)
  : QTreeView(0), m_delegate(delegate)
{
  header()->setVisible(false);
  header()->setStretchLastSection(false);
  setIconSize(QSize(16, 16));
  setSelectionBehavior(SelectItems);
  connect(this, SIGNAL(expanded(const QModelIndex &)),
          SLOT(adjustSize(const QModelIndex &)));
  connect(this, SIGNAL(collapsed(const QModelIndex &)),
          SLOT(adjustSize(const QModelIndex &)));
}

// QTreeViewAdapter :: Protected Member Function. ------------------------------

/*!
 * \brief Adjusts tree column size.
 *
 * \param index the model index
 */
void QTreeViewAdapter::adjustSize(const QModelIndex &index)
{
  if (index.isValid()) {
    resizeColumnToContents(0);
  }
}

/*!
 * \inheritDoc
 */
bool QTreeViewAdapter::event(QEvent *e)
{
  bool processed = QTreeView::event(e);
  switch (e->type()) {
    case QEvent::FontChange:
      // falls through
    case QEvent::StyleChange:
      QApplication::postEvent(this, new QEvent(QEvent::User));
      break;
    case QEvent::User:
      resizeColumnToContents(0);
      break;
    default:
      break;
  }
  return processed;
}
